cc.Class({
    extends: cc.Component,

    properties: {
        tilePrefab : cc.Prefab
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
    },

    // use this for initialization
    onLoad: function () {

    },

    getMeldPos : function () {
        var x = this.node.childrenCount * 181;
        var p = cc.p(x, 0);

        return p;
    },

    chi : function (MJIds) {
        var meld = cc.instantiate(this.tilePrefab);
            meld.position = this.getMeldPos();
        
        this.node.addChild(meld);
        meld.getComponent("BottomMeld").init("chi", MJIds);

    },

    peng : function (MJId) {
        var meld = cc.instantiate(this.tilePrefab);
            meld.position = this.getMeldPos();

        this.node.addChild(meld);
        meld.getComponent("BottomMeld").init("peng", MJId);
    },

    gang : function (MJId, type) {
        if (type !== 3) {//1:明杠,2:暗杠,3:补杠
            var meld = cc.instantiate(this.tilePrefab);
                meld.position = this.getMeldPos();

            this.node.addChild(meld);
            meld.getComponent("BottomMeld").init("gang", MJId, type === 2);
        }else{
            var melds = this.node.children;
            for(var i in melds){
                var meld = melds[i].getComponent("BottomMeld");
                if(meld.type === "peng" && meld.MJId == MJId){
                    meld.fillBar();
                    return;
                }
            }
        }
    },

    actions : function (act, MJIds, type) {
        switch (act) {
            case "chi":
                this.chi(MJIds);
                break;
            case "peng":
                this.peng(MJIds);
                break;
            case "gang":
                this.gang(MJIds, type);
                break;
        }
    }
});
